package com.atguigu.tingshu.dispatch.job;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class DispatchHandler {

    @Autowired
    private SearchFeignClient searchFeignClient;


    @Autowired
    private UserFeignClient userFeignClient;

    @XxlJob("hellojob")
    public void hello() {
        //1.获取任务参数
        String jobParam = XxlJobHelper.getJobParam();
        Map<String, String> map = JSON.parseObject(jobParam, Map.class);
        log.info("任务参数：{}", map);
    }


    /**
     * 定时任务：每个小时触发一次，用于更新小时榜Redis数据
     * 更新ES中各个分类下各个维度热度较高的专辑列表
     */
    @XxlJob("updateLatelyAlbumRanking")
    public void updateLatelyAlbumRanking() {
        log.info("[定时任务服务]更新小时榜任务执行了..");
        searchFeignClient.updateLatelyAlbumRanking();
    }

    /**
     * 定时任务：每天凌晨0点触发一次，用于更新过期会员标识
     */
    @XxlJob("updateVipExpireStatus")
    public void updateVipExpireStatus() {
        log.info("[定时任务服务]新过期会员标识任务执行了..");
        userFeignClient.updateVipExpireStatus();
    }


    /**
     * 分片任务
     */
    @XxlJob("shardjob")
    public void shardjob() {
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();
        log.info("分片参数，总分片数：{}，当前分片索引：{}", shardTotal, shardIndex);
    }
}
